﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class EditCategoryImage : System.Web.UI.UserControl
    {
        public string nameslide;
        public string title;
        public string imageslide;
        public string alt;
        public int slideid;
        public string filePathSlide;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                slideid = Convert.ToInt32(Request.QueryString["idslide"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_CategoryAnh", "CategoryAnh_ID", slideid.ToString());
                nameslide = dt.Rows[0]["CategoryAnh_TieuDe"].ToString();
                title = dt.Rows[0]["CategoryAnh_Title"].ToString();
                imageslide = dt.Rows[0]["CategoryAnh_Image"].ToString();
                alt = dt.Rows[0]["CategoryAnh_Alt"].ToString();
                txtName.Text = nameslide;
                txtTitle.Text = title;
                txtAlt.Text = alt;
            }
            catch (Exception)
            {
                Response.Redirect("/Admin/error.htm");
            }
        }

        protected void btnUpdateSlide_Click(object sender, EventArgs e)
        {
            if (fulImageSlide.HasFile)
            {
                if (LIB.Validate.CheckFileType(fulImageSlide.FileName))
                {
                    string linkimage = fulImageSlide.FileName;
                    LIB.Validate saveimage = new LIB.Validate();
                    saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-CategoryAnh/"), linkimage, 940, fulImageSlide.PostedFile.InputStream);
                    filePathSlide = "/Resources/Images/Images-CategoryAnh/" + fulImageSlide.FileName;

                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[4];
                    cl[0] = new DAL.DHelpers.Column("CategoryAnh_TieuDe", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[1] = new DAL.DHelpers.Column("CategoryAnh_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[2] = new DAL.DHelpers.Column("CategoryAnh_Image", filePathSlide, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[3] = new DAL.DHelpers.Column("CategoryAnh_Alt", txtAlt.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    string[] colWhere = new string[1];
                    colWhere[0] = "CategoryAnh_ID";

                    object[] colValue = new object[1];
                    colValue[0] = slideid;

                    DAL.DCollector.DBCollector.Instance.Update("tbl_CategoryAnh", cl, colWhere, colValue);
                    Response.Redirect("/Admin/Index.aspx?panel=5&sub=9");
                }
                else
                {
                    Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                }
            }
            else
            {
                DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[3];
                cl[0] = new DAL.DHelpers.Column("CategoryAnh_TieuDe", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                cl[1] = new DAL.DHelpers.Column("CategoryAnh_Title", txtTitle.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                cl[2] = new DAL.DHelpers.Column("CategoryAnh_Alt", txtAlt.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                string[] colWhere = new string[1];
                colWhere[0] = "CategoryAnh_ID";

                object[] colValue = new object[1];
                colValue[0] = slideid;

                DAL.DCollector.DBCollector.Instance.Update("tbl_CategoryAnh", cl, colWhere, colValue);
                Response.Redirect("/Admin/Index.aspx?panel=5&sub=9");
            }
        }
    }
}